package cn.jly.spring.boot.cloud.utils;

import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

/**
 * @author lanyangji
 * @description 数据校验工具类
 * @date 2020/11/25 下午 3:58
 */
public class DataValidateUtil {
    private DataValidateUtil() {
    }

    public static String validate(BindingResult result) {
        StringBuilder msg = new StringBuilder(32);

        if (result.hasErrors()) {
            for (ObjectError allError : result.getAllErrors()) {
                msg.append(allError.getDefaultMessage()).append(",");
            }
        }
        // 去除末尾多余的逗号
        return msg.length() > 0 ? msg.substring(0, msg.length() - 1) : "";
    }
}
